package com.ufind.server.facade.impl.assembler;

import com.ufind.server.core.domain.User;
import com.ufind.server.facade.dto.UserDTO;
import com.ufind.server.facade.dto.request.RegisterCommand;
import org.springframework.beans.BeanUtils;

/**
 * 用户装配器
 */
public class UserAssembler {

    /**
     * 将User实体转化为数据传输对象
     */
    public static User toUser(UserDTO userDTO) {
        if (userDTO == null) {
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return user;
    }

    /**
     * 将数据传输对象转换为数据实体
     */
    public static UserDTO toUserDTO(User user) {
        if (user == null) {
            return null;
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }

    /**
     * 将注册命令转化为user实体对象
     * @param command
     * @return
     */
    public static User toCommandUser(RegisterCommand command) {
        User user = new User();
        user.setUsername(command.getUsername());
        user.setPassword(command.getPassword());
        return user;
    }
}